﻿#include "MaterialEditor.h"
#include "MaterialEditorWindow.h"
#include "EditorCamera.h"


LPOYRENDERER		g_pRenderer				= NULL;
LPOYRENDERDEVICE	g_pDevice				= NULL;
OyTimer*			g_pTimer				= NULL;
float				g_Aspect				= 1280.0f/1024.0f;
UINT				g_DefaultSkin			= 0xFFFFFFFF;
UINT				g_DefaultMaterialID		= 0;
UINT				g_DefaultModifierID		= 0xFFFFFFFF;
UINT				g_GuidSB				= 0xFFFFFFFF;
UINT				g_AxisSB				= 0xFFFFFFFF;

OY_UCOLOR_VERTEX_TYPE*	g_GuidVertex			= NULL;
OY_UCOLOR_VERTEX_TYPE*	g_AxisVertex			= NULL;
OY_UTAN_VERTEX_TYPE*	g_RenderObjVertex		= NULL;
WORD*					g_RenderObjIndex		= NULL;
CMaterialEditorCamera	g_pEditorCamera;
TCHAR					g_DiffuseTexPath[256]	= L"DefaultTexture.png";
TCHAR					g_NormalTexPath[256]	= L"\0";

int					g_customMaterialID		= 0;


 HRESULT ProgramStartup(TCHAR *chAPI, const HWND& MainHandle, const HWND* pHandles, const int HandlesNum)
{   
	// no opengl render device yet...
	if( _tcscmp(chAPI, L"OpenGL") == 0 ) 
		return S_OK;

	// create a render object
	g_pRenderer = new OyRenderer(0);
   
	// create a device for the chosen api
	if( FAILED( g_pRenderer->CreateDevice(chAPI) ) ) 
		return E_FAIL;
   
	// get a pointer on that device
	g_pDevice = g_pRenderer->GetDevice();
	if(g_pDevice == NULL)
		return E_FAIL;


	// init render device
	if( FAILED(g_pDevice->Init(MainHandle, pHandles, HandlesNum, 24, 8)) )
		return E_FAIL;

	g_pDevice->UseWindow(0);

	// Init Camera
	g_pDevice->SetMode(EMD_PERSPECTIVE);
	g_pDevice->SetViewPort(NULL);
	g_pDevice->SetClippingPlanes(0.1f, 1000.0f);	
	g_pDevice->SetProjMatrixParams(float_Pi*0.25f, g_Aspect);

	g_pEditorCamera.SetPos(0, 100, -100);
	g_pEditorCamera.SetLookAt(0, 0, 0);
	g_pEditorCamera.SetRotation(-float_Pi/4, float_Pi/4, 0);

	// init Timer
	g_pTimer = new OyTimer(10,10);

	// init Font
	if( FAILED( g_pDevice->CreateFontInManager( "font1", "../Font/YaHei_Consolas.ttf", true, false, 16 ) ) )
		return E_FAIL;

	g_pDevice->PreCacheChar( L"FPSxyz" );

	// Init Guid
	g_AxisVertex = new OY_UCOLOR_VERTEX_TYPE [6];
	g_GuidVertex = new OY_UCOLOR_VERTEX_TYPE [17*2*2];
	int vertIndex = 0;
	for( int Row=-8; Row<=8; Row++ )
	{
		g_GuidVertex[vertIndex].x = Row * 5.0f;
		g_GuidVertex[vertIndex].y = 0;
		g_GuidVertex[vertIndex].z = -8 * 5.0f;
		
		g_GuidVertex[vertIndex].VertexColor = (Row == 0) ? 0xFF000000 : 0xFF969696;
		g_GuidVertex[vertIndex].tu = g_GuidVertex[vertIndex].tv = 0;
		vertIndex++;

		g_GuidVertex[vertIndex].x = Row * 5.0f;
		g_GuidVertex[vertIndex].y = 0;
		g_GuidVertex[vertIndex].z = 8 * 5.0f;
		
		g_GuidVertex[vertIndex].VertexColor = (Row == 0) ? 0xFF000000 : 0xFF969696;
		g_GuidVertex[vertIndex].tu = g_GuidVertex[vertIndex].tv = 0;
		vertIndex++;
	}

	for( int Col=-8; Col<=8; Col++ )
	{
		g_GuidVertex[vertIndex].x = -8 * 5.0f;
		g_GuidVertex[vertIndex].y = 0;
		g_GuidVertex[vertIndex].z = Col * 5.0f;
		
		g_GuidVertex[vertIndex].VertexColor = (Col == 0) ? 0xFF000000 : 0xFF969696;
		g_GuidVertex[vertIndex].tu = g_GuidVertex[vertIndex].tv = 0;
		vertIndex++;

		g_GuidVertex[vertIndex].x = 8 * 5.0f;
		g_GuidVertex[vertIndex].y = 0;
		g_GuidVertex[vertIndex].z = Col * 5.0f;
		
		g_GuidVertex[vertIndex].VertexColor = (Col == 0) ? 0xFF000000 : 0xFF969696;
		g_GuidVertex[vertIndex].tu = g_GuidVertex[vertIndex].tv = 0;
		vertIndex++;
	}

	//////////////////////////////////////////////////////////////////////////////////

	g_RenderObjVertex = new OY_UTAN_VERTEX_TYPE [24];
	memset( g_RenderObjVertex, 0, 24*sizeof(OY_UTAN_VERTEX_TYPE) );
	g_RenderObjVertex[0].x = -10.0f;
	g_RenderObjVertex[0].y =  10.0f;
	g_RenderObjVertex[0].z = -10.0f;
	g_RenderObjVertex[0].vcNormal[0] = 0;
	g_RenderObjVertex[0].vcNormal[1] = 0;
	g_RenderObjVertex[0].vcNormal[2] = -1.0f;
	//g_RenderObjVertex[0].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[0].tu = 0;
	g_RenderObjVertex[0].tv = 0;
	g_RenderObjVertex[1].x =  10.0f;
	g_RenderObjVertex[1].y =  10.0f;
	g_RenderObjVertex[1].z = -10.0f;
	g_RenderObjVertex[1].vcNormal[0] = 0;
	g_RenderObjVertex[1].vcNormal[1] = 0;
	g_RenderObjVertex[1].vcNormal[2] = -1.0f;
	//g_RenderObjVertex[1].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[1].tu = 1.0f;
	g_RenderObjVertex[1].tv = 0;
	g_RenderObjVertex[2].x = -10.0f;
	g_RenderObjVertex[2].y = -10.0f;
	g_RenderObjVertex[2].z = -10.0f;
	g_RenderObjVertex[2].vcNormal[0] = 0;
	g_RenderObjVertex[2].vcNormal[1] = 0;
	g_RenderObjVertex[2].vcNormal[2] = -1.0f;
	//g_RenderObjVertex[2].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[2].tu = 0;
	g_RenderObjVertex[2].tv = 1.0f;
	g_RenderObjVertex[3].x =  10.0f;
	g_RenderObjVertex[3].y = -10.0f;
	g_RenderObjVertex[3].z = -10.0f;
	g_RenderObjVertex[3].vcNormal[0] = 0;
	g_RenderObjVertex[3].vcNormal[1] = 0;
	g_RenderObjVertex[3].vcNormal[2] = -1.0f;
	//g_RenderObjVertex[3].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[3].tu = 1.0f;
	g_RenderObjVertex[3].tv = 1.0f;

	g_RenderObjVertex[4].x =  10.0f;
	g_RenderObjVertex[4].y =  10.0f;
	g_RenderObjVertex[4].z = -10.0f;
	g_RenderObjVertex[4].vcNormal[0] = 1.0f;
	g_RenderObjVertex[4].vcNormal[1] = 0;
	g_RenderObjVertex[4].vcNormal[2] = 0;
	//g_RenderObjVertex[4].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[4].tu = 0;
	g_RenderObjVertex[4].tv = 0;
	g_RenderObjVertex[5].x =  10.0f;
	g_RenderObjVertex[5].y =  10.0f;
	g_RenderObjVertex[5].z =  10.0f;
	g_RenderObjVertex[5].vcNormal[0] = 1.0f;
	g_RenderObjVertex[5].vcNormal[1] = 0;
	g_RenderObjVertex[5].vcNormal[2] = 0;
	//g_RenderObjVertex[5].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[5].tu = 1;
	g_RenderObjVertex[5].tv = 0;
	g_RenderObjVertex[6].x =  10.0f;
	g_RenderObjVertex[6].y = -10.0f;
	g_RenderObjVertex[6].z = -10.0f;
	g_RenderObjVertex[6].vcNormal[0] = 1.0f;
	g_RenderObjVertex[6].vcNormal[1] = 0;
	g_RenderObjVertex[6].vcNormal[2] = 0;
	//g_RenderObjVertex[6].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[6].tu = 0;
	g_RenderObjVertex[6].tv = 1;
	g_RenderObjVertex[7].x =  10.0f;
	g_RenderObjVertex[7].y = -10.0f;
	g_RenderObjVertex[7].z =  10.0f;
	g_RenderObjVertex[7].vcNormal[0] = 1.0f;
	g_RenderObjVertex[7].vcNormal[1] = 0;
	g_RenderObjVertex[7].vcNormal[2] = 0;
	//g_RenderObjVertex[7].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[7].tu = 1;
	g_RenderObjVertex[7].tv = 1;

	g_RenderObjVertex[8].x =  10.0f;
	g_RenderObjVertex[8].y =  10.0f;
	g_RenderObjVertex[8].z =  10.0f;
	g_RenderObjVertex[8].vcNormal[0] = 0;
	g_RenderObjVertex[8].vcNormal[1] = 0;
	g_RenderObjVertex[8].vcNormal[2] = 1.0f;
	//g_RenderObjVertex[8].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[8].tu = 0;
	g_RenderObjVertex[8].tv = 0;
	g_RenderObjVertex[9].x = -10.0f;
	g_RenderObjVertex[9].y =  10.0f;
	g_RenderObjVertex[9].z =  10.0f;
	g_RenderObjVertex[9].vcNormal[0] = 0;
	g_RenderObjVertex[9].vcNormal[1] = 0;
	g_RenderObjVertex[9].vcNormal[2] = 1.0f;
	//g_RenderObjVertex[9].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[9].tu = 1;
	g_RenderObjVertex[9].tv = 0;
	g_RenderObjVertex[10].x =  10.0f;
	g_RenderObjVertex[10].y = -10.0f;
	g_RenderObjVertex[10].z =  10.0f;
	g_RenderObjVertex[10].vcNormal[0] = 0;
	g_RenderObjVertex[10].vcNormal[1] = 0;
	g_RenderObjVertex[10].vcNormal[2] = 1.0f;
	//g_RenderObjVertex[10].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[10].tu = 0;
	g_RenderObjVertex[10].tv = 1;
	g_RenderObjVertex[11].x = -10.0f;
	g_RenderObjVertex[11].y = -10.0f;
	g_RenderObjVertex[11].z =  10.0f;
	g_RenderObjVertex[11].vcNormal[0] = 0;
	g_RenderObjVertex[11].vcNormal[1] = 0;
	g_RenderObjVertex[11].vcNormal[2] = 1.0f;
	//g_RenderObjVertex[11].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[11].tu = 1;
	g_RenderObjVertex[11].tv = 1;

	g_RenderObjVertex[12].x = -10.0f;
	g_RenderObjVertex[12].y =  10.0f;
	g_RenderObjVertex[12].z =  10.0f;
	g_RenderObjVertex[12].vcNormal[0] = -1.0f;
	g_RenderObjVertex[12].vcNormal[1] = 0;
	g_RenderObjVertex[12].vcNormal[2] = 0;
	//g_RenderObjVertex[12].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[12].tu = 0;
	g_RenderObjVertex[12].tv = 0;
	g_RenderObjVertex[13].x = -10.0f;
	g_RenderObjVertex[13].y =  10.0f;
	g_RenderObjVertex[13].z = -10.0f;
	g_RenderObjVertex[13].vcNormal[0] = -1.0f;
	g_RenderObjVertex[13].vcNormal[1] = 0;
	g_RenderObjVertex[13].vcNormal[2] = 0;
	//g_RenderObjVertex[13].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[13].tu = 1;
	g_RenderObjVertex[13].tv = 0;
	g_RenderObjVertex[14].x = -10.0f;
	g_RenderObjVertex[14].y = -10.0f;
	g_RenderObjVertex[14].z =  10.0f;
	g_RenderObjVertex[14].vcNormal[0] = -1.0f;
	g_RenderObjVertex[14].vcNormal[1] = 0;
	g_RenderObjVertex[14].vcNormal[2] = 0;
	//g_RenderObjVertex[14].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[14].tu = 0;
	g_RenderObjVertex[14].tv = 1;
	g_RenderObjVertex[15].x = -10.0f;
	g_RenderObjVertex[15].y = -10.0f;
	g_RenderObjVertex[15].z = -10.0f;
	g_RenderObjVertex[15].vcNormal[0] = -1.0f;
	g_RenderObjVertex[15].vcNormal[1] = 0;
	g_RenderObjVertex[15].vcNormal[2] = 0;
	//g_RenderObjVertex[15].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[15].tu = 1;
	g_RenderObjVertex[15].tv = 1;

	g_RenderObjVertex[16].x = -10.0f;
	g_RenderObjVertex[16].y =  10.0f;
	g_RenderObjVertex[16].z =  10.0f;
	g_RenderObjVertex[16].vcNormal[0] = 0;
	g_RenderObjVertex[16].vcNormal[1] = 1.0f;
	g_RenderObjVertex[16].vcNormal[2] = 0;
	//g_RenderObjVertex[16].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[16].tu = 0;
	g_RenderObjVertex[16].tv = 0;
	g_RenderObjVertex[17].x =  10.0f;
	g_RenderObjVertex[17].y =  10.0f;
	g_RenderObjVertex[17].z =  10.0f;
	g_RenderObjVertex[17].vcNormal[0] = 0;
	g_RenderObjVertex[17].vcNormal[1] = 1.0f;
	g_RenderObjVertex[17].vcNormal[2] = 0;
	//g_RenderObjVertex[17].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[17].tu = 1;
	g_RenderObjVertex[17].tv = 0;
	g_RenderObjVertex[18].x = -10.0f;
	g_RenderObjVertex[18].y =  10.0f;
	g_RenderObjVertex[18].z = -10.0f;
	g_RenderObjVertex[18].vcNormal[0] = 0;
	g_RenderObjVertex[18].vcNormal[1] = 1.0f;
	g_RenderObjVertex[18].vcNormal[2] = 0;
	//g_RenderObjVertex[18].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[18].tu = 0;
	g_RenderObjVertex[18].tv = 1;
	g_RenderObjVertex[19].x =  10.0f;
	g_RenderObjVertex[19].y =  10.0f;
	g_RenderObjVertex[19].z = -10.0f;
	g_RenderObjVertex[19].vcNormal[0] = 0;
	g_RenderObjVertex[19].vcNormal[1] = 1.0f;
	g_RenderObjVertex[19].vcNormal[2] = 0;
	//g_RenderObjVertex[19].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[19].tu = 1;
	g_RenderObjVertex[19].tv = 1;

	g_RenderObjVertex[20].x = -10.0f;
	g_RenderObjVertex[20].y = -10.0f;
	g_RenderObjVertex[20].z = -10.0f;
	g_RenderObjVertex[20].vcNormal[0] = 0;
	g_RenderObjVertex[20].vcNormal[1] = -1.0f;
	g_RenderObjVertex[20].vcNormal[2] = 0;
	//g_RenderObjVertex[20].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[20].tu = 0;
	g_RenderObjVertex[20].tv = 0;
	g_RenderObjVertex[21].x =  10.0f;
	g_RenderObjVertex[21].y = -10.0f;
	g_RenderObjVertex[21].z = -10.0f;
	g_RenderObjVertex[21].vcNormal[0] = 0;
	g_RenderObjVertex[21].vcNormal[1] = -1.0f;
	g_RenderObjVertex[21].vcNormal[2] = 0;
	//g_RenderObjVertex[21].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[21].tu = 1;
	g_RenderObjVertex[21].tv = 0;
	g_RenderObjVertex[22].x = -10.0f;
	g_RenderObjVertex[22].y = -10.0f;
	g_RenderObjVertex[22].z =  10.0f;
	g_RenderObjVertex[22].vcNormal[0] = 0;
	g_RenderObjVertex[22].vcNormal[1] = -1.0f;
	g_RenderObjVertex[22].vcNormal[2] = 0;
	//g_RenderObjVertex[22].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[22].tu = 0;
	g_RenderObjVertex[22].tv = 1;
	g_RenderObjVertex[23].x =  10.0f;
	g_RenderObjVertex[23].y = -10.0f;
	g_RenderObjVertex[23].z =  10.0f;
	g_RenderObjVertex[23].vcNormal[0] = 0;
	g_RenderObjVertex[23].vcNormal[1] = -1.0f;
	g_RenderObjVertex[23].vcNormal[2] = 0;
	//g_RenderObjVertex[23].VertexColor = 0xFFFFFFFF;
	g_RenderObjVertex[23].tu = 1;
	g_RenderObjVertex[23].tv = 1;

	g_RenderObjIndex = new WORD [36];
	int idx = 0;
	g_RenderObjIndex[idx++]  = 0; g_RenderObjIndex[idx++]  = 1; g_RenderObjIndex[idx++]   = 2;
	g_RenderObjIndex[idx++]  = 1; g_RenderObjIndex[idx++]  = 3; g_RenderObjIndex[idx++]   = 2;
	g_RenderObjIndex[idx++]  = 4; g_RenderObjIndex[idx++]  = 5; g_RenderObjIndex[idx++]   = 6;
	g_RenderObjIndex[idx++]  = 5; g_RenderObjIndex[idx++]  = 7; g_RenderObjIndex[idx++]   = 6;
	
	g_RenderObjIndex[idx++]  = 8; g_RenderObjIndex[idx++]   = 9; g_RenderObjIndex[idx++]  = 10;
	g_RenderObjIndex[idx++]  = 9; g_RenderObjIndex[idx++]  = 11; g_RenderObjIndex[idx++]  = 10;
	g_RenderObjIndex[idx++] = 12; g_RenderObjIndex[idx++]  = 13; g_RenderObjIndex[idx++]  = 14;
	g_RenderObjIndex[idx++] = 13; g_RenderObjIndex[idx++]  = 15; g_RenderObjIndex[idx++]  = 14;

	g_RenderObjIndex[idx++] = 16; g_RenderObjIndex[idx++] = 17; g_RenderObjIndex[idx++]  = 18;
	g_RenderObjIndex[idx++] = 17; g_RenderObjIndex[idx++] = 19; g_RenderObjIndex[idx++]  = 18;
	g_RenderObjIndex[idx++] = 20; g_RenderObjIndex[idx++] = 21; g_RenderObjIndex[idx++]  = 22;
	g_RenderObjIndex[idx++] = 21; g_RenderObjIndex[idx++] = 23; g_RenderObjIndex[idx++]  = 22;
	

	// load default skin
	OyColor clr( 0.1f, 1.0f, 0.0f, 1.0f );
	OyColor clrW( 0.0f, 0.0f, 0.0f, 1.0f );
	g_pDevice->GetSkinManager()->AddSkin(&clr, &clr, &clrW, &clrW, 0.0f, &g_DefaultSkin);
	g_pDevice->GetSkinManager()->AddTexture(g_DefaultSkin, g_DiffuseTexPath, 0);
	
	return S_OK;
}

HRESULT ProgramCleanup(void) 
{
	if(g_pRenderer) 
	{
		delete g_pRenderer;
		g_pRenderer = NULL;
	}

	if(g_pTimer)
	{
		delete g_pTimer;
		g_pTimer = NULL;
    }

	if( g_GuidVertex )
		delete [] g_GuidVertex;
	g_GuidVertex = NULL;
	if( g_AxisVertex )
		delete [] g_AxisVertex;
	g_AxisVertex = NULL;
	if( g_RenderObjVertex )
		delete [] g_RenderObjVertex;
	g_RenderObjVertex = NULL;
	if( g_RenderObjIndex )
		delete [] g_RenderObjIndex;
	g_RenderObjIndex = NULL;

	return S_OK;
}


void EditorD3DCanvas::newD3D9ContextCreated()
{
	HWND hMainWnd = (HWND)(this->getParentComponent()->getWindowHandle());
	HWND hChildWnd[1];
	hChildWnd[0] = m_D3D9Context.GetNativeContextHandle();
	if( FAILED( ProgramStartup( L"Direct3D9", hMainWnd, hChildWnd, 1 )) ) 
	{
		MessageBox( NULL, L"error: ProgramStartup() failed", L"Startup Direct3D Error", MB_OK );
	}
	else
	{
		g_pDevice->SetClearColor(g_EditorBKColor.getFloatRed(), g_EditorBKColor.getFloatGreen(), g_EditorBKColor.getFloatBlue()/*0.82745f, 0.82745f, 0.82745f*/);
	}
}

void EditorD3DCanvas::D3D9ContextClosing()
{
	ProgramCleanup();
}

void EditorD3DCanvas::mouseWheelMove (const MouseEvent& , const MouseWheelDetails& wheel)
{
	g_pEditorCamera.AddZoom(wheel.deltaY * 100.0f);
}

void EditorD3DCanvas::mouseDown (const MouseEvent& e)
{
	if( e.mods.isMiddleButtonDown() ) 
	{
		draggablePan.mouseDown (e.getPosition());
	}
	else if( e.mods.isRightButtonDown() )
	{
		draggableOrientation.mouseDown (e.getPosition());
	}
}

void EditorD3DCanvas::mouseDrag (const MouseEvent& e)
{
	if( e.mods.isMiddleButtonDown() )
		draggablePan.mouseDrag (e.getPosition());
	else if( e.mods.isRightButtonDown() )
		draggableOrientation.mouseDrag (e.getPosition());
}

void EditorD3DCanvas::renderD3D9()
{
	if(g_pDevice->IsRunning()) 
	{
		g_pTimer->Update();

		// Pan
		Point<float> cameraPan = draggablePan.getPan();
		draggablePan.ResetPan();
		g_pEditorCamera.SetPanX(-cameraPan.x * 50.0f);
		g_pEditorCamera.SetPanY(-cameraPan.y * 50.0f);

		// Rotation
		Point<float> cameraRot = draggableOrientation.getPan();
		draggableOrientation.ResetPan();
		g_pEditorCamera.SetRotationSpeedX(cameraRot.y * 100.0f);
		g_pEditorCamera.SetRotationSpeedY(cameraRot.x * 100.0f);


		g_pEditorCamera.Update( g_pTimer->GetElapsed() );	// this function must before BeginRendering
		
		g_pEditorCamera.SetRotationSpeedX(0);				// reset rotation
		g_pEditorCamera.SetRotationSpeedY(0);

		// update Camera position
		g_pDevice->SetViewMatrix3D( OyVector4D( g_pEditorCamera.GetRight() ),
			OyVector4D( g_pEditorCamera.GetUp() ),
            OyVector4D( g_pEditorCamera.GetDir() ),
            OyVector4D( g_pEditorCamera.GetPos() ) );

		if( m_bNeedReset )
		{
			g_pDevice->OnDeviceReset();

			// After Reset Device, viewport and Aspect could be changed
			g_pDevice->SetViewPort(NULL);
			g_Aspect = (float)getWidth() / (float)getHeight();
			g_pDevice->SetProjMatrixParams(float_Pi*0.25f, g_Aspect);

			m_bNeedReset = false;
			return;
		}

		g_pDevice->BeginRendering(true, true, true);
		{			
			// Render Terrin Grid
			OyColor GuidColor(1,1,1,1);
			g_pDevice->GetVertexCacheManager()->RenderLines(
				VID_UCOLOR,
				17*2*2,
				g_GuidVertex,
				&GuidColor,
				false);

						
			//Matrix3D<float> RotMat = draggableOrientation.getRotationMatrix();
			//OyMatrix RotationMat( (float*)(&RotMat.mat[0]) );
			
			// Render Objs
			RenderObj();

			// Render Coordinate Axis
			RenderCoordinateAxis();			

			g_pDevice->BeginRenderText();
			// Render Coordinate Lables
			RenderLableXYZ(g_AxisVertex[1].x, g_AxisVertex[1].y, g_AxisVertex[1].z,
				g_AxisVertex[3].x, g_AxisVertex[3].y, g_AxisVertex[3].z,
				g_AxisVertex[5].x, g_AxisVertex[5].y, g_AxisVertex[5].z);
			g_pDevice->DrawTextInPos( 10, 10, OY_DRAWTEXT_SHADOW, 16, 200, 200, 200, L"FPS=\t%4.2f %f %f %f", g_pTimer->GetFPS(), g_AxisVertex[1].x, g_AxisVertex[1].y, g_AxisVertex[1].z );
			g_pDevice->EndRenderText();
		}
		g_pDevice->EndRendering();	

	}
}

void EditorD3DCanvas::RenderObj(void)
{
	if( g_currentTabName.equalsIgnoreCase(L"Material Preview") )
	{
		g_customMaterialStr = String::empty;
		g_SourceCodeStr[0] = 0;
		g_pDevice->GetVertexCacheManager()->RenderTriangles(VID_UTAN, 24, 36,
			g_RenderObjVertex, g_RenderObjIndex, 
			g_DefaultMaterialID, g_DefaultModifierID, NULL,
			g_DefaultSkin);
	}
	else
	{
		SetCustomMaterialStr();
		strcpy_s(g_SourceCodeStr, g_customMaterialStr.toUTF8().getAddress());
		g_pDevice->GetVertexCacheManager()->RenderTriangles(VID_UTAN, 24, 36,
			g_RenderObjVertex, g_RenderObjIndex, 
			g_customMaterialID, g_DefaultModifierID, g_customMaterialStr.toUTF8().getAddress(),
			g_DefaultSkin);
	}
}											 

void EditorD3DCanvas::RenderCoordinateAxis(void)
{
	OyMatrix matView, matProj, matInvViewProj;
	OyVector4D OrigTo;

	// proj position
	OyVector4D vAxisOrig( -0.8f, -0.8f, 0.1f);
	g_pDevice->GetViewMatrix(&matView._11);
	g_pDevice->GetProjMatrix(&matProj._11);

	// Inverse view*proj to get "proj->world" transform
	matInvViewProj.InverseOf( matView*matProj );


	// move the obj to leftbottom of camera then transfer to world coordinate
	OyVector4D Orig = vAxisOrig * matInvViewProj;
	

	// draw the lines stand for coordinate
	const float LINE_LENGTH = 0.005f;

	// RED X
	OrigTo = Orig + OyVector4D(LINE_LENGTH,0,0);
	g_AxisVertex[0].x = Orig.x;
	g_AxisVertex[0].y = Orig.y;
	g_AxisVertex[0].z = Orig.z;
	g_AxisVertex[0].VertexColor = 0xFFFF0000;
	g_AxisVertex[0].tu = g_AxisVertex[0].tv = 0;
	g_AxisVertex[1].x = OrigTo.x;
	g_AxisVertex[1].y = OrigTo.y;
	g_AxisVertex[1].z = OrigTo.z;
	g_AxisVertex[1].VertexColor = 0xFFFF0000;
	g_AxisVertex[1].tu = g_AxisVertex[1].tv = 0;

	// GREEN Y
	OrigTo = Orig + OyVector4D(0,LINE_LENGTH,0);
	g_AxisVertex[2].x = Orig.x;
	g_AxisVertex[2].y = Orig.y;
	g_AxisVertex[2].z = Orig.z;
	g_AxisVertex[2].VertexColor = 0xFF00FF00;
	g_AxisVertex[2].tu = g_AxisVertex[2].tv = 0;
	g_AxisVertex[3].x = OrigTo.x;
	g_AxisVertex[3].y = OrigTo.y;
	g_AxisVertex[3].z = OrigTo.z;
	g_AxisVertex[3].VertexColor = 0xFF00FF00;
	g_AxisVertex[3].tu = g_AxisVertex[3].tv = 0;

	// BLUE Z
	OrigTo = Orig + OyVector4D(0,0,LINE_LENGTH);
	g_AxisVertex[4].x = Orig.x;
	g_AxisVertex[4].y = Orig.y;
	g_AxisVertex[4].z = Orig.z;
	g_AxisVertex[4].VertexColor = 0xFF0000FF;
	g_AxisVertex[4].tu = g_AxisVertex[4].tv = 0;
	g_AxisVertex[5].x = OrigTo.x;
	g_AxisVertex[5].y = OrigTo.y;
	g_AxisVertex[5].z = OrigTo.z;
	g_AxisVertex[5].VertexColor = 0xFF0000FF;
	g_AxisVertex[5].tu = g_AxisVertex[5].tv = 0;
	OyColor LineColor(1,1,1,1);
	g_pDevice->GetVertexCacheManager()->RenderLines(
		VID_UCOLOR,
		6,
		g_AxisVertex,
		&LineColor,
		false);
}

void EditorD3DCanvas::RenderLableXYZ(float LableX_x, float LableX_y, float LableX_z,
	float LableY_x, float LableY_y, float LableY_z,
	float LableZ_x, float LableZ_y, float LableZ_z)
{
	OyMatrix matView, matProj;

	g_pDevice->GetViewMatrix(&matView._11);
	g_pDevice->GetProjMatrix(&matProj._11);

	OY_VIEWPORT ViewPort;
	g_pDevice->GetViewPort(&ViewPort);

	//draw x, y, z label
	OyMatrix matWorldToScreen;
	matWorldToScreen =  matView * matProj;
	OyVector4D Lable_x(LableX_x, LableX_y, LableX_z);
	Lable_x = Lable_x * matWorldToScreen;
	g_pDevice->DrawTextInPos( int((Lable_x.x+1)*0.5f*ViewPort.Width), int(ViewPort.Height-(Lable_x.y+1)*0.5f*ViewPort.Height-16), 0, 16, 255, 0, 0, L"x" );
	OyVector4D Lable_y(LableY_x, LableY_y, LableY_z);
	Lable_y = Lable_y * matWorldToScreen;
	g_pDevice->DrawTextInPos( int((Lable_y.x+1)*0.5f*ViewPort.Width), int(ViewPort.Height-(Lable_y.y+1)*0.5f*ViewPort.Height-16), 0, 16, 0, 255, 0, L"y" );
	OyVector4D Lable_z(LableZ_x, LableZ_y, LableZ_z);
	Lable_z = Lable_z * matWorldToScreen;
	g_pDevice->DrawTextInPos( int((Lable_z.x+1)*0.5f*ViewPort.Width), int(ViewPort.Height-(Lable_z.y+1)*0.5f*ViewPort.Height-16), 0, 16, 0, 0, 255, L"z" );

}